/*
 * @Author: your name
 * @Date: 2020-10-19 17:54:20
 * @LastEditTime: 2021-06-15 10:25:57
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \sende_nuxt\middleware\permission.js
 */
/**
 * 路由鉴定,一些页面游客无权限访问，需要重新登陆
 * @param next
 * @param route
 * @param store
 * @return {Promise<void>}
 * @description 路由中间件
 */
import { getUrlParam } from "~/utils/validate";
export default async function({ next, params, query, route, store, redirect, $axios }) {
  // 不重定向白名单
  // eslint-disable-next-line no-unused-vars
  const whiteList = [
    '/',
    '/login',
    '/register'
  ]
  if (store.state.token) {
    if (route.path === '/login' ||
        route.path === '/register' ||
        route.path === '/login/forget') {
      // 返回首页
      next('/')
    } else {
      // 请求申请个人信息接口 => 当有Token的时候
      // store.dispatch('GetUserInfo')
    }
  } else {
    // if (whiteList.indexOf(route.path) === -1) {
    //   redirect('/login?redirect=' + route.path) // 否则全部重定向到登录页
    // }
  }
  // 存储当前路由路径
  let page = await getUrlParam(route.fullPath, Object.keys(query)[0]);
  if(page) {
    let arr = [];
    // 判断是否存在子级
    if (/\-?/i.test(page)) {
        arr = page.split("-");
    } else {
        arr.push(page);
    }
    store.commit('content/SET_ROUTE', arr)
  }
}
